/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.OrderOpLogsPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * 订单操作日志
 * @ClassName:  OrderOpLogsPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class OrderOpLogsSqlProvider {
	public static final String TABLE_NAME = "order_op_logs";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param orderOpLogs
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderOpLogss(final OrderOpLogsPo orderOpLogs) {
		return new CustomerSQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			if (null != orderOpLogs.getOperation()) {
				WHERE("operation=#{operation}");
			}
			if (null != orderOpLogs.getOrderId()) {
				WHERE("order_id=#{orderId}");
			}
			if (null != orderOpLogs.getStatus()) {
				WHERE("status=#{status}");
			}
			if (null != orderOpLogs.getOperatorId()) {
				WHERE("operator_id=#{operatorId}");
			}
			if(StringUtils.isNotBlank(orderOpLogs.getSortField())) {
				ORDER_BY(orderOpLogs.getSortField() + (StringUtils.isNotBlank(orderOpLogs.getSortMethod()) ? "desc" : orderOpLogs.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != orderOpLogs.getStart() && null != orderOpLogs.getSize()) {
				LIMIT(orderOpLogs.getStart(), orderOpLogs.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param orderOpLogs
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderOpLogsCount(final OrderOpLogsPo orderOpLogs) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);

			if (null != orderOpLogs.getOperation()) {
				WHERE("operation=#{operation}");
			}
			if (null != orderOpLogs.getOrderId()) {
				WHERE("order_id=#{orderId}");
			}
			if (null != orderOpLogs.getStatus()) {
				WHERE("status=#{status}");
			}
			if (null != orderOpLogs.getOperatorId()) {
				WHERE("operator_id=#{operatorId}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderOpLogsById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param orderOpLogs
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addOrderOpLogs(final OrderOpLogsPo orderOpLogs) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);

			if (null != orderOpLogs.getOperation()) {
				VALUES("operation","#{operation}");
			}
			if (StringUtils.isNotBlank(orderOpLogs.getOperationName())) {
				VALUES("operation_name","#{operationName}");
			}
			if (null != orderOpLogs.getOrderId()) {
				VALUES("order_id","#{orderId}");
			}
			if (null != orderOpLogs.getStatus()) {
				VALUES("status","#{status}");
			}
			if (StringUtils.isNotBlank(orderOpLogs.getStatusName())) {
				VALUES("status_name","#{statusName}");
			}
			if (null != orderOpLogs.getOperatorId()) {
				VALUES("operator_id","#{operatorId}");
			}
			if (StringUtils.isNotBlank(orderOpLogs.getOperatorName())) {
				VALUES("operator_name","#{operatorName}");
			}
			if (StringUtils.isNotBlank(orderOpLogs.getDescription())) {
				VALUES("description","#{description}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param orderOpLogs
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateOrderOpLogs(final OrderOpLogsPo orderOpLogs) {
		return new SQL() {{
			UPDATE(TABLE_NAME);

			if (null != orderOpLogs.getOperation()) {
				SET("operation=#{operation}");
			}
			if (null != orderOpLogs.getOrderId()) {
				SET("order_id=#{orderId}");
			}
			if (null != orderOpLogs.getStatus()) {
				SET("status=#{status}");
			}
			if (null != orderOpLogs.getOperatorId()) {
				SET("operator_id=#{operatorId}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delOrderOpLogs(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}

	/**
	 * 批量增加订单操作日志信息
	 * 
	 * @param map	
	 * @Author 迷蝶
	 * @return String
	 */
	public String insertBatch(final Map<String, List<OrderOpLogsPo>> map) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        sb.append("INSERT INTO ").append(TABLE_NAME).append(" (order_id,status,status_name,operation,operation_name,"
        		+ "description,operator_id,operator_name) VALUES ");
        for (OrderOpLogsPo opLog : map.get("opLogs")) {
        	if(i > 0) {sb.append(",");}
        	i++;
        	sb.append("(").append(opLog.getOrderId()).append(",")
        	.append(opLog.getStatus()).append(",'")
        	.append(opLog.getStatusName()).append("',")
        	.append(opLog.getOperation()).append(",'")
        	.append(opLog.getOperationName()).append("','")
        	.append(opLog.getDescription()).append("',")
        	.append(opLog.getOperatorId()).append(",'")
        	.append(opLog.getOperatorName()).append("'")
        	.append(")");
        }
        return sb.toString();
	}
}
